package com.ben.xinyue.seckill.service;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.ben.xinyue.seckill.domain.entity.MiaoshaGoods;
import com.ben.xinyue.seckill.domain.vo.GoodsVo;
import com.ben.xinyue.seckill.mapper.MiaoshaGoodsMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class MiaoshaGoodsService  {

    private final MiaoshaGoodsMapper miaoshaGoodsMapper;

    /**
     * 减库存
     * @param goodsVo 商品
     */
    public boolean reduceStock(GoodsVo goodsVo) {
        MiaoshaGoods miaoshaGoods = new MiaoshaGoods();
        miaoshaGoods.setStockCount(goodsVo.getStockCount()-1);
        UpdateWrapper wrapper = new UpdateWrapper();
        wrapper.ge("goods_id", goodsVo.getId());
        int i = miaoshaGoodsMapper.update(miaoshaGoods,wrapper);
        return i>0;
    }
}